<?php

//to show errors
ini_set('display_errors', 1);

$totfacCount = 0;

$totgsCount = 0;
$totmasCount = 0;
$totarnCount = 0;
$totgsincCount = 0;
$totgscitCount = 0;
$totscoCount = 0;

$xmlFileArray = array("data/all_faculty.xml");

foreach ($xmlFileArray as $xmlFile) {
	if (file_exists($xmlFile)) {

		$xml = simplexml_load_file($xmlFile);

	} else {
		
		echo $xmlFile." File does not exist!";
		exit;
		
	}

	foreach($xml->school as $school) {	
		$schoolName = $school->attributes()->name;
		
		print $schoolName."<br \>";
		
		$facCount = 0;

		$gsSum = 0;
		$masSum = 0;
		$arnSum = 0;
		$gsincSum = 0;
		$gscitSum = 0;
		$scoSum = 0;

		$gsCount = 0;
		$masCount = 0;
		$arnCount = 0;
		$gsincCount = 0;
		$gscitCount = 0;
		$scoCount = 0;
		
		$gsList = array();
		$masList = array();
		$arnList = array();
		$gsincList = array();
		$gscitList = array();
		$scoList = array();
		
		
		
		foreach($school->position as $position) {
			$posName = $position->attributes()->name;
			foreach($position->faculty as $faculty) {
				$facultyName = $faculty->attributes()->name;
			
				$facCount++;
			
				$gsIndex = $faculty->xpath("index[@name='GoogleScholar']");
				$masIndex = $faculty->xpath("index[@name='MicrosoftAcademic']");
				$arnIndex = $faculty->xpath("index[@name='Arnetminer']");
				$gsincIndex = $faculty->xpath("index[@name='GoogleScholarInclusive']");
				$gscitIndex = $faculty->xpath("index[@name='GoogleCitation']");
				$scoIndex = $faculty->xpath("index[@name='Scopus']");

			
				if(!empty($gsIndex)) {
					$h = $gsIndex[0]->attributes()->h;
					if(!empty($h)) {
						$gsCount++;
						$gsSum += $h;
						
						array_push($gsList, $h);
					}
				}
				
				if(!empty($masIndex)) {
					$h = $masIndex[0]->attributes()->h;
					if(!empty($h)) {
						$masCount++;
						$masSum += $h;
						
						array_push($masList, $h);
					}
				}
				
				if(!empty($arnIndex)) {
					$h = $arnIndex[0]->attributes()->h;
					if(!empty($h)) {
						$arnCount++;
						$arnSum += $h;
						
						array_push($arnList, $h);
					}
				}
			
			
				if(!empty($gsincIndex)) {
					$h = $gsincIndex[0]->attributes()->h;
					if(!empty($h)) {
						$gsincCount++;
						$gsincSum += $h;
						
						array_push($gsincList, $h);
					}
				}
				
				if(!empty($gscitIndex)) {
					$h = $gscitIndex[0]->attributes()->h;
					if(!empty($h)) {
						$gscitCount++;
						$gscitSum += $h;
						
						array_push($gscitList, $h);
					}
				}
				
				if(!empty($scoIndex)) {
					$h = $scoIndex[0]->attributes()->h;
					if(!empty($h)) {
						$scoCount++;
						$scoSum += $h;
						
						array_push($scoList, $h);
					} 
				}

			
			}
		}
		
		rsort($gsList, SORT_NUMERIC);
		rsort($masList, SORT_NUMERIC);
		rsort($arnList, SORT_NUMERIC);
		rsort($gsincList, SORT_NUMERIC);
		rsort($gscitList, SORT_NUMERIC);
		rsort($scoList, SORT_NUMERIC);

		
		$gsMax = 0;
		$masMax = 0;
		$arnMax = 0;
		$gsincMax = 0;
		$gscitMax = 0;
		$scoMax = 0;
		
		
		foreach($gsList as $key=>$value) {
			
			if($value>$gsMax)
				$gsMax++;
		}
		
		foreach($masList as $key=>$value) {
			
			if($value>$masMax)
				$masMax++;
		}
		
		foreach($arnList as $key=>$value) {
			
			if($value>$arnMax)
				$arnMax++;
		}
		
		foreach($gsincList as $key=>$value) {
			
			if($value>$gsincMax)
				$gsincMax++;
		}
		
		foreach($gscitList as $key=>$value) {
			
			if($value>$gscitMax)
				$gscitMax++;
		}
		
		foreach($scoList as $key=>$value) {
			
			if($value>$scoMax)
				$scoMax++;
		}
	
		if(!isset($school->attributes()->fac_count)) {
			$school->addAttribute('fac_count',$facCount);
		} else {
			$school->attributes()->fac_count = $facCount;
		}
		
		if(!isset($school->attributes()->gs_sum)){
			$school->addAttribute('gs_sum',$gsSum);
		} else {
			$school->attributes()->gs_sum = $gsSum;
		}
		
		if(!isset($school->attributes()->gs_count)){
			$school->addAttribute('gs_count',$gsCount);
		} else {
			$school->attributes()->gs_count = $gsCount;
		}
		
		if($gsCount > 0) {
			$gsAvg = $gsSum/$gsCount;
		} else {
			$gsAvg = 0;
		}
		if(!isset($school->attributes()->gs_avg)){
			$school->addAttribute('gs_avg',$gsAvg);
		} else {
			$school->attributes()->gs_avg = $gsAvg;
		}
		
		if(!isset($school->attributes()->gs_suc)){
			$school->addAttribute('gs_suc',$gsMax);
		} else {
			$school->attributes()->gs_suc = $gsMax;
		}
		
		if(!isset($school->attributes()->mas_sum)){
			$school->addAttribute('mas_sum',$masSum);
		} else {
			$school->attributes()->mas_sum = $masSum;
		}
		
		if(!isset($school->attributes()->mas_count)){
			$school->addAttribute('mas_count',$masCount);
		} else {
			$school->attributes()->mas_count = $masCount;
		}
		
		if($masCount > 0) {
			$masAvg = $masSum/$masCount;
		} else {
			$masAvg = 0;
		}
		if(!isset($school->attributes()->mas_avg)){
			$school->addAttribute('mas_avg',$masAvg);
		} else {
			$school->attributes()->mas_avg = $masAvg;
		}
		
		if(!isset($school->attributes()->mas_suc)){
			$school->addAttribute('mas_suc',$masMax);
		} else {
			$school->attributes()->mas_suc = $masMax;
		}
		
		if(!isset($school->attributes()->arn_sum)){
			$school->addAttribute('arn_sum',$arnSum);
		} else {
			$school->attributes()->arn_sum = $arnSum;
		}
		
		if(!isset($school->attributes()->arn_count)){
			$school->addAttribute('arn_count',$arnCount);
		} else {
			$school->attributes()->arn_count = $arnCount;
		}
		
		if($arnCount > 0) {
			$arnAvg = $arnSum/$arnCount;
		} else {
			$arnAvg = 0;
		}
		if(!isset($school->attributes()->arn_avg)){
			$school->addAttribute('arn_avg',$arnAvg);
		} else {
			$school->attributes()->arn_avg = $arnAvg;
		}
		
		if(!isset($school->attributes()->arn_suc)){
			$school->addAttribute('arn_suc',$arnMax);
		} else {
			$school->attributes()->arn_suc = $arnMax;
		}
		
		if(!isset($school->attributes()->gsinc_sum)){
			$school->addAttribute('gsinc_sum',$gsincSum);
		} else {
			$school->attributes()->gsinc_sum = $gsincSum;
		}
		
		if(!isset($school->attributes()->gsinc_count)){
			$school->addAttribute('gsinc_count',$gsincCount);
		} else {
			$school->attributes()->gsinc_count = $gsincCount;
		}
		
		if($gsincCount > 0) {
			$gsincAvg = $gsincSum/$gsincCount;
		} else {
			$gsincAvg = 0;
		}
		if(!isset($school->attributes()->gsinc_avg)){
			$school->addAttribute('gsinc_avg',$gsincAvg);
		} else {
			$school->attributes()->gsinc_avg = $gsincAvg;
		}
		
		if(!isset($school->attributes()->gsinc_suc)){
			$school->addAttribute('gsinc_suc',$gsincMax);
		} else {
			$school->attributes()->gsinc_suc = $gsincMax;
		}
		
		if(!isset($school->attributes()->gscit_sum)){
			$school->addAttribute('gscit_sum',$gscitSum);
		} else {
			$school->attributes()->gscit_sum = $gscitSum;
		}
		
		if(!isset($school->attributes()->gscit_count)){
			$school->addAttribute('gscit_count',$gscitCount);
		} else {
			$school->attributes()->gscit_count = $gscitCount;
		}
		
		if($gscitCount > 0) {
			$gscitAvg = $gscitSum/$gscitCount;
		} else {
			$gscitAvg = 0;
		}
		if(!isset($school->attributes()->gscit_avg)){
			$school->addAttribute('gscit_avg',$gscitAvg);
		} else {
			$school->attributes()->gscit_avg = $gscitAvg;
		}
		
		if(!isset($school->attributes()->gscit_suc)){
			$school->addAttribute('gscit_suc',$gscitMax);
		} else {
			$school->attributes()->gscit_suc = $gscitMax;
		}
		
		if(!isset($school->attributes()->sco_sum)){
			$school->addAttribute('sco_sum',$scoSum);
		} else {
			$school->attributes()->sco_sum = $scoSum;
		}
		
		if(!isset($school->attributes()->sco_count)){
			$school->addAttribute('sco_count',$scoCount);
		} else {
			$school->attributes()->sco_count = $scoCount;
		}
		
		if($scoCount > 0) {
			$scoAvg = $scoSum/$scoCount;
		} else {
			$scoAvg = 0;
		}
		if(!isset($school->attributes()->sco_avg)){
			$school->addAttribute('sco_avg',$scoAvg);
		} else {
			$school->attributes()->sco_avg = $scoAvg;
		}
		
		if(!isset($school->attributes()->sco_suc)){
			$school->addAttribute('sco_suc',$scoMax);
		} else {
			$school->attributes()->sco_suc = $scoMax;
		}
		
		$totfacCount += $facCount;

		$totgsCount += $gsCount;
		$totmasCount += $masCount;
		$totarnCount += $arnCount;
		$totgsincCount += $gsincCount;
		$totgscitCount += $gscitCount;
		$totscoCount += $scoCount;
		
		
	}

print "Total Faculty Names: $totfacCount <br />";
print "Google Scholar: $totgsCount collected, ".$totgsCount/$totfacCount."% <br />";
print "Microsoft Academic: $totmasCount collected, ".$totmasCount/$totfacCount."% <br />";
print "Arnetminer.org: $totarnCount collected, ".$totarnCount/$totfacCount."% <br />";
print "Google Scholar Inclusive: $totgsincCount collected, ".$totgsincCount/$totfacCount."% <br />";
print "Google Citation: $totgscitCount collected, ".$totgscitCount/$totfacCount."% <br />";
print "Scopus: $totscoCount collected, ".$totscoCount/$totfacCount."% <br />";

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save($xmlFile);

}

?>